package com.zyh.controller;

import com.zyh.entity.Authority;
import com.zyh.entity.Book;
import com.zyh.entity.User;
import com.zyh.service.AuthorityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class AuthorityController {
    @Autowired
    AuthorityService authorityService;

    @GetMapping("/selectAuthority")
    @ResponseBody
    public Map<String, Object> selectAuthority(Authority authority) {
        if (authority.getPage()==0){
            authority.setPage(1);
        }
        Integer start=(authority.getPage()-1)*authority.getLimit();
        authority.setStart(start);
        List<Authority> authority1 = authorityService.selectAuthority(authority);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", authorityService.countAuthority(authority));
        map.put("data", authority1);
        return map;
    }

    @PostMapping("/updateAuthorityWithId")
    @ResponseBody
    public  Map<String, Object> updateAuthorityWithId(Authority authority) {
        Map<String, Object> map = new HashMap<>();
        Integer integer = authorityService.updateAuthorityWithId(authority);
        map.put("code", 0);
        map.put("msg", "修改成功");
        map.put("count", 1);
        map.put("data", integer);
        return map;
    }


    @GetMapping("/selectAuthorityByid{roleId}")
    @ResponseBody
    public Map<String, Object> selectAuthorityByid(@PathVariable("roleId") Integer id) {
        Authority selectAuthorityByid = authorityService.selectAuthorityByid(id);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","请求成功");
        map.put("count",1);
        map.put("data",selectAuthorityByid);
        return map;
    }
    //添加
    @PostMapping("/addAuthority")
    @ResponseBody
    public  Map<String, Object> addAuthority(Authority authority) {
        Map<String, Object> map = new HashMap<>();
        Integer integer = authorityService.addAuthority(authority);
        map.put("code", 0);
        map.put("msg", "添加成功");
        map.put("count", 1);
        map.put("data", integer);
        return map;

    }

    //删除
    @GetMapping("/DeleteAuthorityByids")
    @ResponseBody
    public  Map<String, Object> DeleteAuthorityByids(Integer[] ids) {
        Map<String, Object> map = new HashMap<>();
        Integer idd=null;
        idd = authorityService.DeleteAuthorityByids(ids);
        map.put("code", 0);
        map.put("msg", "删除成功");
        map.put("count", 1);
        map.put("data", idd);
        return map;
    }


    @GetMapping("/selectAuthorityBywhere")
    @ResponseBody
    public Map<String, Object> selectAuthorityBywhere(Authority authority) {
        List<Authority> authorities = authorityService.selectAuthorityBywhere(authority);
        Map<String, Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","请求成功");
        map.put("count",authorityService.countAuthority(authority));
        map.put("data",authorities);
        return map;
    }
}
